package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.*;
import org.example.models.R;
import org.example.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/interviewschedule")
public class InterviewscheduleController {
    @Autowired
    private InterviewscheduleService interviewscheduleService;
    @Autowired
    private CandidateresumeService candidateresumeService;
    @Autowired
    private InterviewinterviewersService interviewinterviewersService;
    @Autowired
    private RecruitmentrequirementService recruitmentrequirementService;
    @Autowired
    private InterviewrecordService interviewrecordService;
    @Autowired
    private CommentsService commentsService;
    @Autowired
    private DataoperationrecordsheetService dataoperationrecordsheetService;
    @GetMapping("/queryStatusPending")
    public R<Page<Candidateresume>> queryResumeRequirementsBasedOnStatus(@RequestParam(value = "status", required = false) String status,
                                                                         @RequestParam(value = "candidatename", required = false) String candidatename,
                                                                         @RequestParam(value = "intendedPosition", required = false) String intendedPosition,
                                                                         @RequestParam(value = "current", required = false, defaultValue = "1") int current,
                                                                         @RequestParam(value = "size", required = false, defaultValue = "15") int size

    ) {

        Page<Candidateresume> page = new Page<>(current, size);
        List<Candidateresume> interviewschedules = candidateresumeService.queryResumeRequirementsBasedOnStatus(status, candidatename, intendedPosition);
        int start = (current - 1) * size; //获取开始索引
        int end = Math.min(start + size, interviewschedules.size()); //获取结束索引
        List<Candidateresume> subList = interviewschedules.subList(start, end); //截取当前页数据
        page.setRecords(subList);//设置当前页数据
        return R.success(page); //返回分页数据

    }
    @GetMapping("/ascendingOrder")
    public R<Page<Candidateresume>> queryResumeRequirementsBasedOnStatusAscendingOrder(@RequestParam(value = "status", required = false) String status,
                                                                         @RequestParam(value = "candidatename", required = false) String candidatename,
                                                                         @RequestParam(value = "intendedPosition", required = false) String intendedPosition,
                                                                         @RequestParam(value = "current", required = false, defaultValue = "1") int current,
                                                                         @RequestParam(value = "size", required = false, defaultValue = "10") int size

    ) {

        Page<Candidateresume> page = new Page<>(current, size);
        List<Candidateresume> interviewschedules = candidateresumeService.queryResumeRequirementsBasedOnStatusAscendingOrder(status, candidatename, intendedPosition);
        int start = (current - 1) * size; //获取开始索引
        int end = Math.min(start + size, interviewschedules.size()); //获取结束索引
        List<Candidateresume> subList = interviewschedules.subList(start, end); //截取当前页数据
        page.setRecords(subList);//设置当前页数据
        return R.success(page); //返回分页数据

    }
    @GetMapping("/descendingOrder") //降序
    public R<Page<Candidateresume>> queryResumeRequirementsBasedOnStatusDescendingOrder(@RequestParam(value = "status", required = false) String status,
                                                                         @RequestParam(value = "candidatename", required = false) String candidatename,
                                                                         @RequestParam(value = "intendedPosition", required = false) String intendedPosition,
                                                                         @RequestParam(value = "current", required = false, defaultValue = "1") int current,
                                                                         @RequestParam(value = "size", required = false, defaultValue = "15") int size

    ) {

        Page<Candidateresume> page = new Page<>(current, size);
        List<Candidateresume> interviewschedules = candidateresumeService.queryResumeRequirementsBasedOnStatusDescendingOrder(status, candidatename, intendedPosition);

        int start = (current - 1) * size; //获取开始索引
        int end = Math.min(start + size, interviewschedules.size()); //获取结束索引
        List<Candidateresume> subList = interviewschedules.subList(start, end); //截取当前页数据
        page.setRecords(subList);//设置当前页数据
        return R.success(page); //返回分页数据

    }
    // 添加
    @PostMapping("/insert")
    public R<Interviewschedule> insert(@RequestBody Interviewschedule interviewschedule,
                                       @RequestAttribute("userId") String userId) {
        boolean save = interviewscheduleService.save(interviewschedule);
       //根据id修改简历状态
        Candidateresume candidateresume = new Candidateresume();
        candidateresume.setCandidateid(interviewschedule.getRecruitmentDemandNumber());
        candidateresume.setResumeStatus("进行中");
        candidateresumeService.updateById(candidateresume);
        Dataoperationrecordsheet dataoperationrecordsheet = new Dataoperationrecordsheet();
        dataoperationrecordsheet.setOperationTargetId(String.valueOf(interviewschedule.getInterviewNumber()));
        dataoperationrecordsheet.setOperationType("添加");
        dataoperationrecordsheet.setOperationTime(new Date());
        dataoperationrecordsheet.setOperatorId(userId);
        dataoperationrecordsheetService.save(dataoperationrecordsheet);
        if (save) {
            List<Interviewinterviewers> interviewinterviewers = new ArrayList<>();
            List<Interviewrecord> interviewrecords = new ArrayList<>();


            for (Integer interviewerId : interviewschedule.getInterviewerIds()) {
                Interviewinterviewers inter = new Interviewinterviewers();
                inter.setInterviewerId(interviewerId);
                inter.setScheduleAnInterviewNumber(interviewschedule.getInterviewNumber());
                inter.setArrangementStatus("进行中");
                interviewinterviewers.add(inter);
                Interviewrecord interviewrecord = new Interviewrecord();
                interviewrecord.setInterviewerNumber(interviewerId);
                interviewrecord.setInterviewSchedulingId(interviewschedule.getInterviewNumber());
                interviewrecord.setCandidateNumber(interviewschedule.getRecruitmentDemandNumber());
                interviewrecord.setInterviewType("线下");
                interviewrecords.add(interviewrecord);
            }
            interviewinterviewersService.saveBatch(interviewinterviewers);
            //面试记录
            interviewrecordService.saveBatch(interviewrecords);

            return R.success( "添加成功");
        }
        return R.error("添加失败");
    }
    @GetMapping ("/queryStatusPendingqueryStatusPending")
    public R<Page<Interviewschedule>> queryStatusPendingqueryStatusPending(@RequestParam(value = "status", required = false) String status,
                                                                         @RequestParam(value = "CandidateName", required = false) String CandidateName,
                                                                         @RequestParam(value = "IntendedPosition", required = false) String IntendedPosition,
                                                                         @RequestParam(value = "current", required = false, defaultValue = "1") int current,
                                                                         @RequestParam( value = "size", required = false, defaultValue = "15") int size
//                                                                           @RequestAttribute("userId") Integer userId
    ){
        Page<Interviewschedule> page = new Page<>(current, size);
        List<Interviewschedule> interviewschedules = interviewscheduleService.queryStatusPendingqueryStatusPending(status, CandidateName, IntendedPosition);
        int start = (current - 1) * size;
        int end = Math.min(start + size, interviewschedules.size());
        List<Interviewschedule> subList = interviewschedules.subList(start, end);
        page.setRecords(subList);
        return R.success(page);
    }

    //查询面试详情
    @GetMapping("/selectInterviewDetailById")
    public R<List<Interviewschedule>> selectInterviewDetailById(@RequestParam(value = "interviewNumber", required = false) Integer interviewNumber){
        List<Interviewschedule> interviewschedules = interviewscheduleService.selectInterviewDetailById(interviewNumber);
        return R.success(interviewschedules);
    }
    //查询面试人员安排
    @GetMapping("/selectInterviewScheduleWithInterviewerAndEmployee")
    public R<List<Interviewschedule>> selectInterviewScheduleWithInterviewerAndEmployee(@RequestParam(value = "interviewNumber", required = false) String interviewNumber,
                                                                                        @RequestParam(value = "interviewerId", required = false) String interviewerId,
                                                                                        @RequestAttribute("userId") Integer userId){
        interviewerId=userId+"";
        List<Interviewschedule> interviewschedules = interviewscheduleService.selectInterviewScheduleWithInterviewerAndEmployee(interviewNumber, interviewerId);
        return R.success(interviewschedules);
    }
}
